package com.sc.byahi.user.client;

import java.util.Arrays;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class PhotoUploadView extends Composite {

	private static PhotoUploadViewUiBinder uiBinder = GWT.create(PhotoUploadViewUiBinder.class);

	interface PhotoUploadViewUiBinder extends UiBinder<Widget, PhotoUploadView> {
	}

	@UiField
	FormPanel formPanel;
	@UiField
	Hidden profileid;
	@UiField
	Hidden email;
	@UiField
	FlexTable files;
	@UiField
	Button submit;
	@UiField
	Button cancel;
	@UiField
	SpanElement error;

	final FileUpload photo1 = new FileUpload();
	final FileUpload photo2 = new FileUpload();
	final FileUpload photo3 = new FileUpload();
	final FileUpload photo4 = new FileUpload();

	public PhotoUploadView(final Long profileId, final boolean isMap) {
		initWidget(uiBinder.createAndBindUi(this));
		GreetingService.Util.getInstance().getUploadURL(new AsyncCallback<String>() {
			public void onSuccess(String url) {
				formPanel.setAction(url); // <<<- BLOBSTORE.REQUESTUPLOADURL HERE!
				formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
				formPanel.setMethod(FormPanel.METHOD_POST);
				profileid.setValue(profileId.toString());
				email.setValue(AppController.get().getOwner().getId());

				files.setWidget(0, 0, photo1);
				files.setWidget(1, 0, photo2);
				files.setWidget(2, 0, photo3);
				files.setWidget(3, 0, photo4);
			}

			public void onFailure(Throwable error) {
				UIUtil.showGenericFailureDialog();
			}

		});

		formPanel.addSubmitHandler(new FormPanel.SubmitHandler() {
			public void onSubmit(SubmitEvent event) {
			}
		});
		formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			public void onSubmitComplete(SubmitCompleteEvent event) {
				// When the form submission is successfully completed, this event is
				// fired. Assuming the service returned a response of type text/html,
				// we can get the result text here (see the FormPanel documentation for
				// further explanation).
				// History.newItem("myprofiles", true);
				// Window.alert(event.getResults());
				AppController.get().profileApprovalChanged(profileId, false);
				AppController.get().profileUpdated(profileId, isMap);
				((PopupPanel) formPanel.getParent().getParent().getParent()).hide();
				UIUtil.showDialog(Arrays.asList("Pictures uploaded successfully"), "Pic Upload");
				// System.out.println(event.getResults());
			}
		});

		photo1.setName("photo1");
		photo2.setName("photo2");
		photo3.setName("photo3");
		photo4.setName("photo4");
		error.setClassName("approved");
	}

	@UiHandler("submit")
	void onClick(ClickEvent e) {
		// validate before submit
		error.setClassName("approved");
		error.setInnerHTML("");

		if (photo1.getFilename().length() == 0 && photo2.getFilename().length() == 0 && photo3.getFilename().length() == 0 && photo4.getFilename().length() == 0) {
			error.setClassName("unapproved");
			error.setInnerHTML("Please upload at least one photo.");
		} else {
			if (photo1.getFilename() != null && photo1.getFilename().trim().length() == 0) {
				files.remove(photo1);
			}
			if (photo2.getFilename() != null && photo2.getFilename().trim().length() == 0) {
				files.remove(photo2);
			}
			if (photo3.getFilename() != null && photo3.getFilename().trim().length() == 0) {
				files.remove(photo3);
			}
			if (photo4.getFilename() != null && photo4.getFilename().trim().length() == 0) {
				files.remove(photo4);
			}
			formPanel.submit();
		}
	}

	@UiHandler("cancel")
	void onClick1(ClickEvent e) {
		((PopupPanel) this.getParent()).hide();
	}
}
